﻿//                       NOTICE
// 
// This software was produced for the U. S. Government
// under Contract No. FA8721-10-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)
// 
// � 2010 The MITRE Corporation. All Rights Reserved. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using MXSE.Renderers.TileManager;

namespace MXSE.MultiTouch.Gestures
{
    public class BoxingGesture : ComplexGesture
    {
        private bool updated = false;

        public BoundingBox box { get; private set; }

        public BoxingGesture(TouchGesture[] touches)
        {
            if (touches.Length != 4)
                throw new Exception("BoxingGesture requires exactly four TouchGestures.");

            for (int i = 0; i < 4; i++)
            {
                AddBasic(touches[i]);
            }

            createBoundingBox();
        }

        //Want this to encompass all the touches
        public void createBoundingBox()
        {
            Vector3[] pos = new Vector3[4];
            for (int i = 0; i < 4; i++)
            {
                pos[i] = Conversions.MouseToWorld(Basics[i].startPosition);
            }

            float xMin, xMax, yMin, yMax;
            xMin = xMax = pos[0].X;
            yMin = yMax = pos[0].Y;
            for (int i = 1; i < 4; i++)
            {
                if (pos[i].X < xMin)
                    xMin = pos[i].X;
                if (pos[i].X > xMax)
                    xMax = pos[i].X;
                if (pos[i].Y < yMin)
                    yMin = pos[i].Y;
                if (pos[i].Y > yMax)
                    yMax = pos[i].Y;
            }

            Vector3 min = new Vector3(xMin, yMin, -5);
            Vector3 max = new Vector3(xMax, yMax, 5);
            box = new BoundingBox(min, max);
        }

        public override void Update()
        {
            if (!updated)
            {
                updated = true;
                base.Update();
            }
        }

        protected override void OnDeath()
        {
            foreach (BasicGesture b in Basics)
                b.Parent = null;
        }
    }
}
